<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>

    <script>

        //删除用的id
        var id;
        var relationId;

        //点赞数量参数
        var likeNumber;


        /**
         * 1代表点过赞了
         * 2代表没点赞(已经取消点赞了)
         */
        var isUpvokeFlag;

        //取消点赞功能
        function deleteUpvoke(id,relationId) {
            $.ajax({
                url:"/upvokes/strategys/" + id +'/' + relationId,
                type: "DELETE",
                success: function (data) {
                    isUpvokeFlag=0;
                    $("#likeBtn").addClass("fa-thumbs-o-up")//实心
                    $("#likeBtn").removeClass("fa-thumbs-up")//空心
                }
            });
        }

        $(function () {
            var params = getParams();

            //当前页的input框
            var currentInput = $("#currentPage");

            //用于存储游记的数组容器
            var travelArr = [];

            //总页数
            var pages = 1;

            if (params.strategyId) {
                relationId = params.strategyId;
                //获取大攻略对象
                $.get('/strategies/' + params.strategyId, function (data) {
                    $(".title").html(data.title);
                    likeNumber=data.likeNum;
                    // $("#likeNumInput").val(data.likeNum)
                    //发送请求判断是否已经登录以及点过赞
                    $.ajaxSettings.async = false;
                    $(".likeNum").html(likeNumber);
                    $.get("/upvokes/" + relationId , {type: 3}, function (data) {
                        if (data.success) {
                            if (data.result == null) {//没点过赞
                                //alert("没点")
                                isUpvokeFlag = 0;
                                $("#likeBtn").addClass("fa-thumbs-o-up");
                                $("#likeBtn").removeClass("fa-thumbs-up")
                            } else {//实心刷新之前是点了赞的
                               // alert("点了")
                                id=data.result.id
                                relationId = data.result.relationId
                                $("#likeBtn").addClass("fa-thumbs-up");
                                $("#likeBtn").removeClass("fa-thumbs-o-up")
                                isUpvokeFlag = 1;
                            }
                        }
                    });
                    $.ajaxSettings.async = true;
                })


                //获取攻略分类集合
                $.get('/strategies/' + params.strategyId + "/catalogs", function (data) {
                    var index = 1;
                    $(".catalogs").renderValues({list: data}, {

                        getName: function (item, value) {
                            $(item).html(index++ + "/" + value);
                        },
                        getHref: getHref
                    });
                })

                //查询 大家都在看 的游记
                $.get('/travels/strategys/' + params.strategyId, function (data) {
                    $(".hot").renderValues(data, {
                        getHref: getHref
                    });
                })
            }
            //点击进入评论页面
            $('#commentBtn').click(function () {
                window.location.href = '/mine/addComment.html?strategyId=' + params.strategyId;
            })
            //攻略评论对象
            var strategyComment;
            strategyCommentQuery();
            function strategyCommentQuery() {
                //获取当前页的值
                var currentPage = currentInput.val();//1
                //查询骡友的评论
                $.get('/strategyComments/{strategyId}/comments', {
                    strategyId: params.strategyId,
                    state: 0,
                    currentPage: currentPage
                }, function (data) {
                    console.log(data);
                    //合并两个数组到第一个参数数组上
                    $.merge(travelArr, data.list);
                    //用于渲染时循环,循环时是取list的属性出来
                    var json = {
                        list: travelArr
                    };
                    $('#comment').renderValues(json, {
                        getSrc: function (item, value) {
                            var temp = '';
                            $.each(value, function (index, ele) {
                                temp += '<li><img src=' + ele + '></li>'
                            })
                            $(item).html(temp);
                        },
                        getHref: getHref
                    })
                    //设置总页数
                    pages = data.pages;
                });
                //当前页input框+1
                currentInput.val(parseInt(currentPage) + 1);
            }


            //当页面滚动条变化时，执行的函数
            $(window).scroll(function () {
                if ($('#pills-comment-tab').hasClass('active')) {
                    if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
                        var currentPage = currentInput.val();
                        //判断是否小于或等于最后一页,如果是就去查询
                        if (currentPage <= pages) {
                            strategyCommentQuery();
                        } else {
                            $(document).dialog({
                                type: 'notice',
                                infoText: '你再多拉点,肠子都出来了!',
                                autoClose: 1000
                            });
                        }
                    }
                }
            });


            $.get('/tags/{state}', {state: 0,strategyId:params.strategyId}, function (data) {
                var json = {
                    tags: data
                }
                $('.tag').renderValues(json)
            })

            //收藏功能
            $.get('/collects',{"strategy.id":params.strategyId},function (data) {
                if (!data.success) {
                    $('#collectBtn').removeClass("fa-heart-o");
                    $('#collectBtn').addClass("fa-heart");
                }
            })

            //收藏游记的点击事件
            $('#collectBtn').click(function () {
                if ($('#collectBtn').hasClass("fa-heart")) {
                    $.ajax({
                        url : "/collects" ,   // 处理的请求路径
                        type : "post" ,      // 此处发送的是delete请求（可变更为其他需要的请
                        data:{_method:"DELETE","strategy.id":params.strategyId},
                        dataType : "json" , // 返回的数据类型为json类型
                        success : function(data) {
                            $('#collectBtn').removeClass("fa-heart");
                            $('#collectBtn').addClass("fa-heart-o");
                        }
                    });
                    $(document).dialog({
                        type : 'notice',
                        infoText: '取消收藏成功',
                        autoClose: 500,
                        position: 'center'  // center: 居中; bottom: 底部
                    });
                }else {
                    $.post('/collects/strategy',{"strategy.id":params.strategyId},function (data) {
                        if(data.success){
                            $(document).dialog({
                                type : 'notice',
                                infoText: '收藏成功',
                                autoClose: 500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                            $('#collectBtn').removeClass("fa-heart-o");
                            $('#collectBtn').addClass("fa-heart");
                        }else {
                            $(document).dialog({
                                type : 'notice',
                                infoText: data.msg,
                                autoClose: 1500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                        }
                    })
                }

            })

            var val = $("#praise").val();
            //点赞功能
            $("#likeBtn").click(function () {
                if(sessionStorage.getItem("user")==null){
                    $(document).dialog({
                        type: 'notice',
                        infoText: '您还没登录,请先登录! ',
                        autoClose: 500,
                        position: 'center'  // center: 居中; bottom: 底部
                    });
                    return;
                }
                if (isUpvokeFlag == 0 & $("#likeBtn").hasClass("fa-thumbs-o-up")) {
                    //点赞功能
                    $.post('/upvokes/strategys',{"relationId":params.strategyId},function (data) {
                        console.log(data);
                        id = data.result.id
                        if(data.success){
                            $("#likeBtn").addClass("fa-thumbs-up")
                            $("#likeBtn").removeClass("fa-thumbs-o-up")
                            isUpvokeFlag=1;
                            likeNumber++;
                            $(".likeNum").html(likeNumber)
                        }
                    })
                }else {
                    //取消点赞功能
                    deleteUpvoke(id,relationId);
                    likeNumber--;
                    $(".likeNum").html(likeNumber)
                }
            });

            //旅行单的功能代码块
            //先查询,这个攻略是否已经加入这个旅行单了
            $.get("/travelForms",{"strategy.id":params.strategyId},function (data) {
                if(!data.success){
                    $('#travelForm').html(data.msg);
                }
            })
            $('#travelForm').click(function () {
                if($('#travelForm').text() == "取消旅行"){
                    $.ajax({
                        url : "/travelForms" ,   // 处理的请求路径
                        type : "post" ,      // 此处发送的是delete请求（可变更为其他需要的请
                        data:{_method:"DELETE","strategy.id":params.strategyId},
                        dataType : "json" , // 返回的数据类型为json类型
                        success : function(data) {
                            $('#travelForm').html("加入旅行单");
                        }
                    });
                    $(document).dialog({
                        type : 'notice',
                        infoText: '取消旅行成功',
                        autoClose: 500,
                        position: 'center'  // center: 居中; bottom: 底部
                    });
                }else {
                    $.post('/travelForms',{"strategy.id":params.strategyId},function (data) {
                        if(data.success){
                            $(document).dialog({
                                type : 'notice',
                                infoText: '快去旅行吧',
                                autoClose: 500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                            $('#travelForm').html("取消旅行");
                        }else {
                            $(document).dialog({
                                type : 'notice',
                                infoText: data.msg,
                                autoClose: 1500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                        }
                    })
                }
            });

            //搜索按钮搜索功能
            $("#serachStrsategy").click(function () {
                if ($("#keyword").val() != null&&$("#keyword").val() !='') {
                    window.location.href="search.html?keyword="+$("#keyword").val()
                }
            });
        })
    </script>


</head>

<body>
<!--点赞用 隐藏域-->
<input id="likeNumInput" type="hidden"/>
<input type="hidden" id="praise"/>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search" id="serachStrsategy">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略" name="keyword" id="keyword">
            </div>
        </div>
    </div>
</div>
<input type="hidden" name="currentPage" id="currentPage" value="1">
<div class="container strategies">
    <h5 class="title">广州攻略</h5>
    <small><span id="showNum"></span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart-o" id="collectBtn"><span> </span> 收藏</i>
       <!-- <i class="fa fa-thumbs-o-up likeNum" id="likeBtn" ><span id="likeNum"> </span> 点赞</i>-->
        <i class="fa fa-thumbs-o-up likeNum" id="likeBtn" render-html="likeNum"></i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div render-loop="result">
                <a data-href="travelContent.html?id=" render-key="result.id" render-fun="getHref">
                    <button class="btn" render-html="result.title"></button>
                </a>
            </div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="getName"></h6>
                    <div render-loop="list.details">
                        <a data-href="strategyDetails.html?id=" render-key="list.details.id" render-fun="getHref">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>

            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div render-loop="tags">
                <span render-html="tags.name"></span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a href="userProfiles.html">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-loop="list.starNums"><i class="fa fa-star"></i></span>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div class="comment-content">
                            <a href="strategyComment.html">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-key="list.imgList" render-fun="getSrc">

                        </ul>

                        <div class="d-flex icon">
                            <a data-href="/mine/strategyComment.html?strategyCommentId=" render-key="list.id" render-fun="getHref">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px"> 点击进行评论 </i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>

</body>
</html>